﻿using System.Collections.Generic;

namespace WebPlayer
{
    public class SessionResources
    {
        private int _mCount = 0;
        private readonly Dictionary<string, string> _mResourceList = new Dictionary<string, string>();
        private readonly Dictionary<string, string> _mResourceKeys = new Dictionary<string, string>();
        
        public string Add(string filename)
        {
            // if adding the same filename, return the existing resource id
            if (_mResourceKeys.ContainsKey(filename))
            {
                return ToUrl(_mResourceKeys[filename]);
            }

            _mCount++;
            var key = _mCount.ToString() + System.IO.Path.GetExtension(filename);
            _mResourceList.Add(key, filename);
            _mResourceKeys.Add(filename, key);
            return ToUrl(key);
        }

        public string ToUrl(string key)
        {
            return "Resource.ashx?id=" + key;
        }

        public string Get(string key)
        {
            string result;
            if (_mResourceList.TryGetValue(key, out result))
            {
                return result;
            }
            return string.Empty;
        }
    }
}